//
//  ICMessageDatabase.h
//  ICome
//
//  Created by ENN on 16/3/7.
//  Copyright © 2016年 iCom. All rights reserved.
//  消息数据库工具类

#import <Foundation/Foundation.h>
#import "ICHeaderFile.h"
#import "ICMessageFrame.h"
@class ICGroup;
@class ICMessageModel;
@class ICGroupUser;
@class ICGtasksModel;
@class ICNotify;
@class ICApplicationModel;
@class ICMessage;
@class LCBMessage;
@class ICUser;


typedef void(^finishedBlock)(NSArray *array,NSError *error);

typedef void (^unReadBlock)(int count);

@interface ICMessageDatabase : NSObject

// save message
+ (BOOL)saveMessage:(ICMessageModel *)messageModel;

// update message
+ (void)updateMessage:(ICMessage *)message identifier:(NSString *)identifier;

// get messages of count
+ (void)selectMessageWithCount:(int)count
                         group:(NSString *)group
                  firstMessage:(ICMessage *)message
                        finish:(finishedBlock)finish;


// get messages of count
+ (void)selectASCMessageWithCount:(int)count
                            group:(NSString *)group
                     firstMessage:(ICMessage *)firstMessage
                           finish:(finishedBlock)finish;
// save group
+ (void)saveGroup:(ICGroup *)group;
// 添加 默认组表
+ (void)saveGroup:(ICGroup *)group isTop:(BOOL)isTop localType:(int)localType;

+ (void)updateGroupInfo:(ICGroup *)group;

// 更改用户信息
+ (void)updateUserPhotoId:(NSString *)photoId
                      eId:(NSString *)eId;

+ (void)selectGroupsIsTop:(BOOL)isTop
                    gType:(NSString *)gType
                   finish:(finishedBlock)finish;

+ (ICMessageFrame *)getMessage:(NSString *)gId
                         msgId:(NSString *)messageId;

// get unread message count
+ (int)getGroupUnreadMessageCountWithGroupId:(NSString *)gId;

+ (void)updateGroupUnreadMessageCountWithGroupId:(NSString *)gId
                                           count:(int)count;
// 修改组状态
+ (void)updateGroupAlertStatus:(NSInteger)status gId:(NSString *)gId;
// save user
+ (void)saveUser:(ICUser *)user;
// 保存用户
+ (void)saveUsers:(NSArray *)users;

// get userName
+ (NSString *)userNameWitheId:(NSString *)eId;
// get gId 通过前缀在关联表中获取数据
+ (NSString *)usergIdWitheId:(NSString *)eId;

+ (void)saveGroupUserWithGroupId:(NSString *)gId
                       groupUser:(ICGroupUser *)groupUser;
+ (void)saveGroupUsersWithGroupId:(NSString *)gId
                       groupUsers:(NSArray *)groupUsers;
// 根据gId删除某条关联关系
+ (void)deleteGroupUserWithGroupId:(NSString *)gId
                               eId:(NSString *)eId;
// top group
+ (void)makeGroupTop:(ICGroup *)group
               isTop:(BOOL)isTop
         lastMsgTime:(NSInteger)lastMsgTime;

// delete group
+ (void)deleteGroupWithGroupId:(NSString *)gId;

// 最近联系人
+ (void)recentContactUsers:(finishedBlock)finish;

// delete message
+ (void)deleteMessage:(ICMessage *)message messageId:(NSString *)messageId;

// change message status
+ (void)changeMessageStatus:(ICMessage *)message
                     status:(ICMessageStatus)messageStatus;

// get userList of group
+ (void)getUsersWithGroupId:(NSString *)gId
                     finish:(finishedBlock)finish;

// get userList of group
+ (void)getUsersWithGroupId:(NSString *)gId
                      limit:(int)limit
                     finish:(finishedBlock)finish;

+ (void)getUsersOfGroupId:(NSString *)gId
                searchTxt:(NSString *)text
                   finish:(finishedBlock)finish;

+ (void)searchUsersWithGroupId:(NSString *)gId
                     searchTxt:(NSString *)text
                        finish:(finishedBlock)finish;

+ (void)getSearchGroups:(NSString *)gId
              searchTxt:(NSString *)text
                 finish:(finishedBlock)finish;

+ (void)getGroupWithgId:(NSString *)gId
                 finish:(finishedBlock)finish;

// update group name
+ (void)updateGroupNameWithGroupId:(NSString *)gId
                             gName:(NSString *)gName;
+ (NSString *)getUserNameWithEId:(NSString *)eId;
+ (NSString *)getPhotoIdWithEId:(NSString *)eId;

+ (NSString *)getUsersWithName:(NSString *)name gId:(NSString *)gId;

+ (void)updateGroupSyn:(NSString *)gId
                 gName:(NSString *)gName
               photoId:(NSString *)photoId;

+ (NSString *)getGroupPhotoIdWithGroupId:(NSString *)gId;

// top status
+ (BOOL)isTopWithGroupId:(NSString *)gId;

// 所有显示的组
+ (void)getGroups:(finishedBlock)finish;

// 正常群组 0 1 2 组
+ (void)getNormalGroups:(finishedBlock)finish;

// 后续获取群组统一使用该方法
+ (void)getGroupsWithgType:(ICGroupType)gType Finish:(finishedBlock)finish;

// master or not
+  (BOOL)isMasterWithGroupId:(NSString *)gId;

+ (void)clearChatRecord:(NSString *)gId;

+ (void)invalidGroup:(NSString *)gId
               valid:(BOOL)valid;

// group valid status for current user
+ (BOOL)isValidWithGroup:(NSString *)gId;

+ (BOOL)isMessageExisted:(NSString *)gId
               messageId:(NSString *)messageId;

+ (void)updateGroupLastMessageTime:(NSString *)gId
                       lastMsgTime:(NSInteger)lastMsgTime
                        lastMsgTxt:(NSString *)lastMsgTxt
                         lastMsgId:(NSString *)lastMsgId
                      lastChangeId:(NSString *)changeId;

// chage group localType
+ (void)changeGroupLocalType:(BOOL)localType
                     GroupId:(NSString *)gId;

+ (BOOL)isGroupExisted:(NSString *)gId;

// the max messageId
+ (NSInteger)maxMessageId:(NSString *)gId;

+ (void)messageBackAction:(NSString *)gId
                messageId:(NSString *)msgId
              lastMsgTime:(NSInteger)lastMsgTime;

+ (void)messageDidRead:(NSString *)gId
                 msgId:(NSString *)msgId;

// 删除关联关系并且把该组状态的valid设为0
+ (void)kickUserAction:(NSString *)gId
            messageTxt:(NSString *)messageTxt;

// 更改自身群主状态
+ (void)changeGroupMaster:(NSString *)gId
                 isMaster:(BOOL)isMaster;

// 更改自身对应群的消息免打扰状态
+ (void)changeGroupBusy:(NSString *)gId
                 isBusy:(BOOL)isBusy;

// busy status
+ (BOOL)isBusyWithGroupId:(NSString *)gId;

// search group
+ (void)searchGroupWithSearchText:(NSString *)text
                           finish:(finishedBlock)finish;

// group type
+ (NSString *)groupTypeWithGroupId:(NSString *)gId;

// did have unreadCount
+ (BOOL)isRedCircleAppear;

// search message
+ (void)searchMessage:(NSString *)gId
            searchTxt:(NSString *)searchTxt
               finish:(finishedBlock)finish;
// 更新某个消息状态
+ (void)updateMessageStatus:(NSString *)messageId
                        gId:(NSString *)gId
                     status:(ICMessageStatus)status;

// get all message
+ (void)selectAllMessage:(NSString *)gId
                  finish:(finishedBlock)finish;


+ (void)getDeliverListIsTop:(BOOL)isTop
                     finish:(finishedBlock)finish;
// 获取送达号列表
+ (void)getDeliverListFinish:(finishedBlock)finish;
// group name
+ (NSString *)getGroupName:(NSString *)gId;

+ (NSInteger)getGroupUserCount:(NSString *)gId;

+ (void)fileMessageWithGid:(NSString *)gId
                    finish:(finishedBlock)finish;

+ (void)searchDeliver:(NSString *)text
               finish:(finishedBlock)finish;

+ (BOOL)isDeliverExist:(NSString *)dId;



// the deliver max messageId
+ (NSInteger)deliverMaxMessageId:(NSString *)dId;
+ (NSString *)deliverLastMessage:(NSString *)dId;

+ (void)clearDeliverMessage:(NSString *)dId;

// update deliverInfo
+ (void)updateDeliverInfo:(NSString *)photoId
                      dId:(NSString *)dId
                    dName:(NSString *)dName
                    valid:(NSInteger)valid;


+ (void)cancelDeliversTopStatus;

// 送达号置顶和顺序
+ (void)updateDeliverTopStatus:(NSString *)dId
                      topOrder:(int)order;

+ (void)updateDeliverUnreadCount:(NSInteger)unReadCount
                             dId:(NSString *)dId;

+ (void)searchDeliverMessage:(NSString *)msgTitle
                         dId:(NSString *)dId
                      finish:(finishedBlock)finish;

+ (void)unTopDeliverStatus:(NSString *)dId;

+ (void)deleteDeliverMessage:(NSString *)dId
                       msgId:(NSString *)msgId;

// delete deliver
+ (void)deleteDeliver:(NSString *)dId;


// 组的lastUpdate
+ (NSNumber *)groupInfoLastUpdate:(NSString *)gId;
+ (void)setGroupInfoLastUpdate:(NSString *)gId
                    lastUpdate:(NSNumber *)lastUpdate;

//+ (void)saveGtasks:(ICGtasksModel *)gtasks;

+ (void)getGtasksListWithGroup:(NSString *)group
                        status:(NSString *)status
                         msgId:(NSInteger)msgId
                         Count:(int)count
                        finish:(finishedBlock)finish;

+ (void)setGtasksMsgId:(NSString *)msgId
                Status:(NSString *)status;
// 获取本地活跃的群组
+ (void)selectGroupsWithgType:(NSString *)gType
                    localType:(NSInteger )localType
                       finish:(finishedBlock)finish;
// 根据gtype 搜索本地的群组 单人或者多人
+ (void)searchGroupsWithSearchText:(NSString *)text
                             gType:(NSString *)gType
                         localType:(NSInteger )localType
                            finish:(finishedBlock)finish;

+ (void)saveDrafts:(NSString *)gId
             draft:(NSString *)draft;

// 如果为空，则表示不存在
+ (NSString *)getDrafts:(NSString *)gId;

+ (void)deleteDrafts:(NSString *)gId;

+ (void)deleteAllMessages;
//+ (void)deleteAllMessages2:(finishedBlock)finish;
// 保存轻应用
+ (void)saveApplication:(ICApplicationModel *)app;
// 更新app归属
+ (void)updateApplicationappClassId:(ICApplicationModel *)app;
// 更新app状态
+ (void)updateApplicationSubs:(ICApplicationModel *)app;
// 应用列表
+ (void)selectApplicationsWithSubs:(NSInteger)Subs
                          SubsType:(NSInteger)subsType
                            finish:(finishedBlock)finish;
// 获取具体某个应用
+ (void)getApplicationWithAppId:(NSString *)appId
                         finish:(finishedBlock)finish;


+ (NSString *)nickNameWithEid:(NSString *)eId
                          gId:(NSString *)gId;
+ (void)updateNickNameWith:(NSString *)eId
                       gId:(NSString *)gId
                     nName:(NSString *)nName;


// 获取具体某个应用 送达号 群组 的基本信息 id photoId name
+ (void)getBaseInfoWithgId:(NSString *)gId
                    finish:(finishedBlock)finish;



// ----------------------------工作台数据操作----------------------------------------------
// 保存轻应用
+ (void)saveJobapplication:(ICApplicationModel *)app;

// 更新轻应用
+ (void)updateJobapplication:(ICApplicationModel *)app;
// 取应用列表
+ (void)selectJobapplicationsWithappvalid:(NSInteger)appvalid
                             appClassId:(NSInteger)appClassId
                             finish:(finishedBlock)finish;



// 获取不同的数据
+ (void)selectJobapplicationsappClassIdWithappvalid:(int)appvalid
                                finish:(finishedBlock)finish;

// 更新app归属
+ (void)updateJobapplicationappClassId:(ICApplicationModel *)app;
// 更新 权重
+ (void)updateJobapplicationWeight:(ICApplicationModel *)app;


// 更新app appvalid
+ (void)updateJobapplicationAppvalid:(int)appvalid;

// 更新app appvalid
+ (void)updateJobapplicationAppvalid:(int)appvalid  withappId:(NSString *)appId;

// 取自己选择应用列表
+ (void)selectJobappChoicelicationsWithappvalid:(NSInteger)appvalid
                                         finish:(finishedBlock)finish;









@end
